using UnityEngine;
using System.Collections;

[System.Serializable]
public class MonsterMove : MonoBehaviour {
	
	private GameObject nav0 = null;
	private GameObject nav1 = null;
	public GameObject monster;
	public float speed = 0.5f;
	public bool debug = false;
	public bool onTrigger = true;
	public bool rotateDebug = false;
	public bool loop = true;
	
	public NavPoint nextNavPoint;
	
	public bool dynamicRotateTowards = false;
	
	[System.NonSerializedAttribute]
	public float startSpeed = 0.0f;
	
	private bool[] navPointsReached;
	
	private static int NAV0 = 0;
	private static int NAV1 = 1;
	
	private int currentNav = 0;
	
	[System.SerializableAttribute]
	public class Debug {
		public static int x = 0;
	}
	
	public bool triggered = false;
	private Vector3 nav0pos;
	private Vector3 nav1pos;
	private Vector3 monsterPos;
	private Vector3 dir;
	private Vector3 navPos;
	
	private bool reachedNav = false;
	
	public RotateTowards rotateTowards;
	
	public bool switchToNextNavPoint = true;
	
	private float sumDeltaTime = 0.0f;
	
	// Use this for initialization
	void Start () {			
		startSpeed = speed;
		dir = nextNavPoint.transform.position - monsterPos;
		/* if (nav0 != null) {
			nav0pos = nav0.transform.position;
		}
		if (nav1 != null) {
			nav1pos = nav1.transform.position;
		} */
		monsterPos = monster.transform.position;
		if (!onTrigger) {
			triggered = true;	
		}
	}
	
	private void updatePos() {
		monsterPos = monster.transform.position;
		
		if (rotateTowards != null) {
			rotateTowards.objectToRotate = monster.transform;
			if (dynamicRotateTowards)	 {
				rotateTowards.target = nextNavPoint.transform;
			}
		}
		
		dir = nextNavPoint.transform.position - monsterPos;
		
		dir.Normalize();
		
		//monster.transform.Translate(dir * speed * Time.deltaTime, Space.World);
		monster.transform.Translate(dir * speed * Time.deltaTime, Space.World);
	}
	
	// Update is called once per frame
	void Update () {
		if (triggered) {				
			updatePos();	
			if (true) {			
				if (Vector3.Distance(monsterPos, nextNavPoint.transform.position) <= nextNavPoint.threshold && switchToNextNavPoint) {				
					if (nextNavPoint.next != null ) {
						nextNavPoint = nextNavPoint.next;
					}
				}		
				
				if (rotateTowards != null) {
					rotateTowards.Update();
				}
			}
		}
	}
	
	void OnTriggerEnter(Collider other) {
		if (other.tag == "Player") {
			triggered = true;
		}
	}
	
	void OnGUI() {
		if (debug) {
			GUI.Box(new Rect(0,0,200,400), 
				"Triggered: " + ((triggered) ? "1" : "0") + "\n " +
				"OnTrigger: " + ((onTrigger) ? "1" : "0") + "\n " +
				"Monster:" +
				"\n " +
				"X:" + monsterPos.x + "" +
				"\n " +
				"Y:" + monsterPos.y + "\n" +
				"Nav: " + nextNavPoint.transform.position + "\n" +
				"Dir: " + dir + "\n" +
				"Nav Name: " + nextNavPoint.name + "\n" +
				"Delta Time: " + sumDeltaTime + "\n" +
				"Distance: " + Vector3.Distance(monsterPos, nextNavPoint.transform.position).ToString("#.##") + "\n" +
				"Threshold: " + (nextNavPoint.threshold));
		}
		
		if (rotateDebug) {
			rotateTowards.OnGUI();
		}
	}
	
	void OnDrawGizmos() {
		Gizmos.DrawWireSphere(transform.position, nextNavPoint.threshold);
		if (nextNavPoint != null) {
			Gizmos.DrawLine(transform.position, nextNavPoint.transform.position);	
		}
	}
}
